package com.dev.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.dev.security.entity.Menu;
import com.dev.security.entity.Org;
import com.dev.security.mapper.MenuMapper;
import com.dev.security.mapper.OrgMapper;
import com.dev.security.service.MenuService;
import com.dev.security.service.OrgService;
import com.dev.security.vo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 机构Service实现
 *
 * @author yuboon
 * @version v1.0
 * @date 2020/04/04
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public int save(MenuVo menuVo) {
        Menu menuEntity = new Menu();
        // hutool
        BeanUtil.copyProperties(menuVo,menuEntity);
        menuEntity.setCreateDate(new Date());
        return menuMapper.insert(menuEntity);
    }

    @Override
    @Transactional
    public int update(MenuVo menuVo) {
        Menu menuEntity = new Menu();

        BeanUtil.copyProperties(menuVo,menuEntity);
        return menuMapper.update(menuEntity);
    }

    @Override
    @Transactional
    public int remove(int id) {
        return menuMapper.remove(id);
    }

    @Override
    public int batchRemove(String ids) {
        JSONArray jsonArray = JSONUtil.parseArray(ids);
        for (Object id : jsonArray) {
            menuMapper.remove(Convert.toInt(id));
        }
        return jsonArray.size();
    }

    @Override
    public PageVo<MenuVo> selectList(MenuVo menuVo, PageVo pageVo) {
        if (StrUtil.isNotEmpty(menuVo.getCode())){
            menuVo.setCode("%" + menuVo.getCode() + "%");
        }
        List<MenuVo> listVo = CollectionUtil.newArrayList();
        Page page = PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());

        Menu menu = new Menu();
        BeanUtil.copyProperties(menuVo,menu);
        List<Menu> listEntity = menuMapper.selectList(menu);
        for(Menu entity : listEntity){
            MenuVo vo = new MenuVo();
            BeanUtil.copyProperties(entity,vo);
            vo.setCreateDate(DateUtil.formatDate(entity.getCreateDate()));
            listVo.add(vo);
        }
        pageVo.setList(listVo);
        pageVo.setTotal(page.getTotal());

        return pageVo;
    }

    @Override
    public List<? extends TreeVo> selectTree() {
        List<Menu> menuList = menuMapper.selectList(new Menu());
        ArrayList<MenuTreeVo> treeVoList = CollectionUtil.newArrayList();

        for (Menu menu : menuList) {
            if (menu.getParentId() == 0){
                MenuTreeVo treeVo = new MenuTreeVo();
                treeVo.setId(menu.getId());
                treeVo.setTitle(menu.getName());
                treeVo.setCode(menu.getCode());
                treeVo.setUrl(menu.getUrl());
                treeVo.setSequence(menu.getSequence());
                treeVo.setParentId(0);
                treeVo.setParentTitle("无");
                treeVo.setIcon(menu.getIcon());
                treeVoList.add(treeVo);
                buildTree(treeVo,menuList);
            }
        }
        return treeVoList;
    }

    private void buildTree(MenuTreeVo parentVo,List<Menu> list){
        for (Menu menu : list) {
            if (menu.getParentId() == parentVo.getId()){
                MenuTreeVo childVo = new MenuTreeVo();
                childVo.setId(menu.getId());
                childVo.setTitle(menu.getName());
                childVo.setCode(menu.getCode());
                childVo.setUrl(menu.getUrl());
                childVo.setSequence(menu.getSequence());
                childVo.setParentId(parentVo.getId());
                childVo.setParentTitle(parentVo.getTitle());
                childVo.setIcon(menu.getIcon());


                List<TreeVo> childList = parentVo.getChildren();
                if (childList == null){
                    childList = CollectionUtil.newArrayList();
                    parentVo.setChildren(childList);
                }

                childList.add(childVo);
                buildTree(childVo,list);
            }
        }
    }

    @Override
    public List<IndexMenuTreeVo> selectIndexTree() {
        //List<Menu> menuList = menuMapper.selectList(new Menu());

        Subject subject = SecurityUtils.getSubject();
        System.err.println("username:" + subject.getPrincipal());

        List<Menu> menuList = menuMapper.selectListByUsername((String) subject.getPrincipal());
        ArrayList<IndexMenuTreeVo> treeVoList = CollectionUtil.newArrayList();

        for (Menu menu : menuList) {
            if (menu.getParentId() == 0){
                IndexMenuTreeVo treeVo = new IndexMenuTreeVo();
                treeVo.setId(menu.getId());
                treeVo.setTitle(menu.getName());
                treeVo.setHref(menu.getUrl());
                treeVo.setIcon(menu.getIcon());
                treeVoList.add(treeVo);
                buildIndexTree(treeVo,menuList);
            }
        }
        return treeVoList;
    }

    private void buildIndexTree(IndexMenuTreeVo parentVo,List<Menu> list){
        for (Menu menu : list) {
            if (menu.getParentId() == parentVo.getId()){
                IndexMenuTreeVo childVo = new IndexMenuTreeVo();
                childVo.setId(menu.getId());
                childVo.setTitle(menu.getName());
                childVo.setHref(menu.getUrl());
                childVo.setIcon(menu.getIcon());

                List<IndexMenuTreeVo> childList = parentVo.getChild();
                if (childList == null){
                    childList = CollectionUtil.newArrayList();
                    parentVo.setChild(childList);
                }

                childList.add(childVo);
                buildIndexTree(childVo,list);
            }
        }
    }



}
